#include "../constant.h"

typedef struct queue // 这里的队列，rear所指的那块不存放数据，用以区分空对/满队
{
    DataType data[Capacity]; // 实际容量为 capacity-1
    int front;               // front入队
    int rear;                // rear出队
} * Queue;

#include "./queue.h"

int isFull(Queue q);

void printf(Queue q)
{
    int i;
    printf("[Queue] [ ");
    for (i = (q->rear + 1) % Capacity;
         i != q->front;
         i = (i + 1) % Capacity)
    {
        printf("%d ", q->data[i]);
    }
    printf("%d ", q->data[i]);
    printf("]\n          ⬆front(deQueue)\n");
}

Queue InitQueue()
{
    printf("[init] ring_seq_queue\n");
    Queue q = (Queue)malloc(sizeof(queue));
    q->front = 0;
    q->rear = 0;
    return q;
}

int Empty(Queue q)
{
    return q->front == q->rear;
}

int isFull(Queue q)
{
    return (q->front + 1) % Capacity == q->rear;
}

int Size(Queue q)
{
    return (q->rear - q->front + Capacity) % Capacity;
}
int Pop(Queue &q)
{
    if (Empty(q))
    {
        warning("already a empty queue\n");
        return 0;
    }
    q->rear = (q->rear + 1) % Capacity;
    return 1;
}

int Push(Queue &q, DataType x)
{
    if (isFull(q))
    {
        warning("full queue\n");
        return 0;
    }
    q->front = (q->front + 1) % Capacity;
    q->data[q->front] = x;
    return 1;
}

DataType Front(Queue q)
{
    if (Empty(q))
    {
        warning("empty queue\n");
        return 0;
    }
    return q->data[q->front];
}

DataType Back(Queue q)
{
    if (Empty(q))
    {
        warning("empty queue\n");
        return 0;
    }
    return q->data[(q->rear + 1) % Capacity];
}
